﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.ServiceModel.Activation;
using LuctecWS.Core;
using LuctecWS.Envelope;
using LuctecWS.Aspect;

namespace LuctecWS
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service1 : IService1
    {
        [ServiceBoundary]
        public EmailTemplateRS GetTemplates(BaseRQ req)
        {
            EmailTemplateRS res = new EmailTemplateRS();

            List<EmailTemplate> emailTemplates = EmailTemplate.GetTemplates();

            foreach (EmailTemplate emailTemplate in emailTemplates)
            {
                if (emailTemplate.Error != null)
                    res.Errors.Add(emailTemplate.Error);
                else
                    res.EmailTemplates.Add(emailTemplate);
            }

            return res;
        }

        [ServiceBoundary]
        public EmailTemplateRS SaveTemplate(EmailTemplateRQ req)
        {
            EmailTemplateRS res = new EmailTemplateRS();

            foreach (EmailTemplate emailTemplate in req.EmailTemplates)
            {
                EmailTemplate emailTemplateRS = emailTemplate.Save();

                if (emailTemplateRS.Error != null)
                    res.Errors.Add(emailTemplateRS.Error);
                else
                    res.EmailTemplates.Add(emailTemplateRS);
            }

            return res;
        }

        [ServiceBoundary]
        public ContactRS GetContacts(BaseRQ req)
        {
            ContactRS res = new ContactRS();

            List<Contact> contacts = Contact.GetContacts();

            foreach (Contact contact in contacts)
            {
                if (contact.Error != null)
                    res.Errors.Add(contact.Error);
                else
                    res.Contacts.Add(contact);
            }

            return res;
        }

        [ServiceBoundary]
        public ContactRS SaveContact(ContactRQ req)
        {
            ContactRS res = new ContactRS();

            foreach (Contact contact in req.Contacts)
            {
                Contact contactRS = contact.Save();

                if (contactRS.Error != null)
                    res.Errors.Add(contactRS.Error);
                else
                    res.Contacts.Add(contactRS);
            }

            return res;
        }

        [ServiceBoundary]
        public EmailListRS GetEmailLists(BaseRQ req)
        {
            EmailListRS res = new EmailListRS();

            List<EmailList> emailLists = EmailList.GetEmailLists();

            foreach (EmailList emailList in emailLists)
            {
                if (emailList.Error != null)
                    res.Errors.Add(emailList.Error);
                else
                    res.EmailLists.Add(emailList);
            }

            return res;
        }

        [ServiceBoundary]
        public EmailListRS SaveEmailList(EmailListRQ req)
        {
            EmailListRS res = new EmailListRS();

            foreach (EmailList emailList in req.EmailLists)
            {
                EmailList emailListRS = emailList.Save();

                if (emailListRS.Error != null)
                    res.Errors.Add(emailListRS.Error);
                else
                    res.EmailLists.Add(emailListRS);
            }

            return res;
        }

        [ServiceBoundary]
        public EmailListContactRS GetEmailListContacts(BaseRQ req)
        {
            EmailListContactRS res = new EmailListContactRS();

            List<EmailListContact> emailListContacts = EmailListContact.GetEmailListContacts();

            foreach (EmailListContact emailListContact in emailListContacts)
            {
                if (emailListContact.Error != null)
                    res.Errors.Add(emailListContact.Error);
                else
                    res.EmailListContacts.Add(emailListContact);
            }

            return res;
        }

        [ServiceBoundary]
        public EmailListContactRS SaveEmailListContact(EmailListContactRQ req)
        {
            EmailListContactRS res = new EmailListContactRS();

            foreach (EmailListContact emailListContact in req.EmailListContacts)
            {
                EmailListContact emailListContactRS = emailListContact.Save();

                if (emailListContactRS.Error != null)
                    res.Errors.Add(emailListContactRS.Error);
                else
                    res.EmailListContacts.Add(emailListContactRS);
            }

            return res;
        }

        public void Teste()
        {
            SendMail.Create();
        }
    }
}
